package com.yunfeng.supercall.activity;

import java.util.ArrayList;
import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.ly.library.stickyadpter.BaseStickyListAdapter;
import com.ly.quickdev.library.view.MySideBar;
import com.ly.quickdev.library.view.MySideBar.OnTouchingLetterChangedListener;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.ContactsBaseBean;

/***
 * 各种带section的列表基类页面
 * 
 * @author 洋
 * 
 * @param <T>
 */
public abstract class BaseStickyHeaderListActivity<T extends ContactsBaseBean>
		extends BaseActivity implements OnItemClickListener {

	protected List<T> mList;
	protected BaseStickyListAdapter<T> mAdapter;
	protected StickyListHeadersListView mListView;
	protected SwipeRefreshLayout mSwipeRefreshLayout;// support v4包赠送的下拉刷新
	private MySideBar mSideBar;// 右边的字母标记
	protected View mLoadingView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sticky_header_list);

		loadData();

	}

	@Override
	public void onSupportContentChanged() {
		super.onSupportContentChanged();
		mSwipeRefreshLayout = findView(R.id.swipe);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				loadData();
			}
		});
		mLoadingView = findViewById(R.id.loading);
		mList = new ArrayList<T>();
		mListView = findView(R.id.list);
		mListView.setOnItemClickListener(this);
		TextView mTextView = findView(R.id.empty);
		mTextView.setText(R.string.no_data);
		mListView.setEmptyView(mTextView);
		mSwipeRefreshLayout.setRefreshing(true);
		mSwipeRefreshLayout.setColorSchemeResources(R.color.main_yellow);
		mSideBar = (MySideBar) findViewById(R.id.side_bar);
		mSideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

			@Override
			public void onTouchingLetterChanged(String s) {
				if (mList == null || mList.isEmpty()) {
					return;
				}
				for (int i = 0, j = mList.size(); i < j; i++) {
					ContactsBaseBean base = mList.get(i);
					if (base.getStickyItem().equalsIgnoreCase(s)) {
						mListView.setSelection(i);
						return;
					}
				}
			}
		});
	}

	public abstract void loadData();

	public void setListAdapter() {
		if (mAdapter == null) {
			mAdapter = new BaseStickyListAdapter<T>(this, mList) {

				@Override
				public View initView(int position, View convertView,
						ViewGroup parent) {
					return BaseStickyHeaderListActivity.this.getView(position,
							convertView, parent);
				}

				@Override
				public View getHeaderView(int position, View convertView,
						ViewGroup parent) {
					return BaseStickyHeaderListActivity.this.getHeaderView(
							position, convertView, parent);
				}

				@Override
				public int getViewTypeCount() {
					return BaseStickyHeaderListActivity.this.getViewTypeCount();
				}

				@Override
				public int getItemViewType(int position) {
					return BaseStickyHeaderListActivity.this
							.getItemViewType(position);
				}
			};
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.notifyDataSetChanged();
		}
		mSwipeRefreshLayout.setRefreshing(false);
		mLoadingView.setVisibility(View.GONE);
		mListView.setVisibility(View.VISIBLE);
	}

	public abstract View getView(int position, View convertView,
			ViewGroup parent);

	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		return ContactsBaseBean.getHeaderView(getActivity(), position, convertView,
				parent, mList);
	}

	public int getViewTypeCount() {
		return 1;
	}

	public int getItemViewType(int position) {
		return 0;
	}
}
